<?php

class CommonHelper
{
    /**
     * get model name such as:content_comment to ContentComment
     * @param string $name content_comment
     * @return string ContentComment
     */
    static public function getModelName($name)
    {
        $modelName = '';
        $name = strtolower($name);
        if(strpos($name, '_'))
        {
            foreach(explode('_', $name) as $_t_)
                $modelName .= ucfirst($_t_);
        }
        else
            $modelName = ucfirst($name);

        return $modelName;
    }
    
    /**
     * get models array by the given url parameters
     * @param float|int $offset
     * @param string $extraCriteriaString the string encode by base64_encode(serialize($extraCriteria))
     * @param string $extraParamsString the string encode by base64_encode(serialize($extraParams))
     * @param EMongoDocument $model Ex.Content::model()
     * @return array of the $model
     */
    static public function getModelsByUrlParams($offset, $extraCriteriaString, $extraParamsString, EMongoDocument $model)
	{
		$offsetField = 'ctime';
        $sortField = 'ctime';
        $criteria = new EMongoCriteria;
        if( ! empty($extraCriteriaString))
        {
            $extraCriteria = unserialize(base64_decode($extraCriteriaString));
            foreach($extraCriteria as $methodName => $paramsArray)
                call_user_func_array(array($criteria, $methodName), $paramsArray);
        }
        
        if( ! empty($extraParamsString))
        {
            $extraParams = unserialize(base64_decode($extraParamsString));
            if(isset($extraParams['methods']) && is_array($extraParams['methods']))
            {
                foreach($extraParams['methods'] as $key=>$value)
                {
                    $params = array();
                    $method = $value;

                    if(is_string($key))
                    {
                        $method = $key;
                        $params = $value;
                    }
                    $model = call_user_func_array(array($model, $method), $params);
                }
            }

            if(isset($extraParams['offsetField']) && is_string($extraParams['offsetField']))
                $offsetField = $extraParams['offsetField'];

            if(isset($extraParams['sortField']) && is_string($extraParams['sortField']))
                $sortField = $extraParams['sortField'];
        }

        //@notice -0.0001 to fix a mongodb bugs.
        $criteria->addCond($offsetField, '<', (float)$offset-0.0001);
        
        if( ! $model->getDbCriteria()->getSort())
            $criteria->sort($sortField, EMongoCriteria::SORT_DESC);
        
        if( ! $criteria->getLimit())
            $criteria->limit(15);
        
        return $model->findAll($criteria);
	}
    
    /**
     * create the params array, be used by getModelsByUrlParams.
     * @param array|'' $models
     * @param array|string $extraCriteria
     * @param array|string $extraParams
     * @return array array('offset'=>$offset, 'extraCriteria'=>$_extraCriteria, 'extraParams'=>$_extraParams);
     */
    static public function createGetModelsUrlParams($models, $extraCriteria=array(), $extraParams=array())
    {
        if('' === $models)
            $offset = microtime(true)+7200;//To fix when the page loaded and user post a new post, then he(she)click any button to show a list, who can't see the new post right now.
        else if( ! empty($models) && is_array($models))
        {
            $lastModel = array_pop($models);
            $offsetField = 'ctime';
			
			if(is_string($extraParams))
				$extraParams = unserialize(base64_decode($extraParams));
			
			if(is_array($extraParams) && isset($extraParams['offsetField']) && is_string($extraParams['offsetField']))
                $offsetField = $extraParams['offsetField'];
			
            $offset = $lastModel->$offsetField;
        }
        else
            $offset = microtime(true);	

        //if it's string now, then no need to run "base64_encode(serialize(...))" again.
    	$_extraCriteria = empty($extraCriteria) ? '' :
            (is_string($extraCriteria) ? $extraCriteria : base64_encode(serialize($extraCriteria)));
    	$_extraParams = empty($extraParams) ? '' :
            (is_string($extraParams) ? $extraParams : base64_encode(serialize($extraParams)));

    	return array('offset'=>$offset, 'extraCriteria'=>$_extraCriteria, 'extraParams'=>$_extraParams);
    }
    
    /**
     * @todo how about there is a same name file?
     * @param string $path
     * @param int $mode default 0777
     * @return boolean
     */
    static public function createFolder($path, $mode=0777)
    {
        if ( ! file_exists($path))
        {
            self::createFolder(dirname($path));
            return mkdir($path, $mode);
        }
        else
            return true;
    }
}

